<?

class Inktype {

	var $CI;

	var $site_id;

	var $site_url_title;

	var $site_name;

	var $site_description;

	var $site_template_folder;
	
	var $site_template_default_view;

	var $view_file;
	
	var $user_display_name;
	
	function Inktype() {

		$this->CI =& get_instance();

		$this->CI->load->model('mdl_sites');

		$this->set_site();

	}

	function anchor($uri, $title = NULL, $attributes = NULL) {

		return anchor($this->site_url_title . '/' . $uri, $title, $attributes);

	}

	function form_open($class, $method, $attributes = NULL) {

		// this function should be called from a view to execute a model's method

		$form = form_open($this->CI->uri->uri_string(), $attributes);

		$form .= form_hidden('class', $class);

		$form .= form_hidden('method', $method);

		if (!$_POST) {

			// if the form has not yet been posted we'll execute the method for $this->validation purposes in view
			$this->CI->$class->$method();

		}

		return $form;

	}

	function format_date($timestamp, $format = 1) {

		switch ($format) {
			case 1:
				return date('Y-m-d g:i:s A', $timestamp);
			case 2:
				return date('Y-m-d', $timestamp);
			case 3:
				return date('l, F j, Y', $timestamp);
			case 4:
				return date('g:i A', $timestamp);
		}

	}

	function is_logged_in() {

		if ($this->CI->session->userdata('user_id')) {

			return TRUE;

		}

		return FALSE;

	}

	function load_view() {

		$view_file = $this->view_file();

		$this->CI->load->view($this->site_template_folder . '/' . $view_file);

	}

	function post_handler() {

		// this will get called at the beginning of each page load

		if (isset($_POST['class']) AND isset($_POST['method'])) {

			$this->CI->$_POST['class']->$_POST['method']();

		}

		elseif (isset($_POST['btn_search'])) {

			redirect($this->site_url_title . '/' . $this->CI->input->post('result_view') . '/hash/' . $this->CI->mdl_search->save_search());

		}

	}

	function set_site() {

		if ($this->CI->is_admin) {
			
			$this->site_template_folder = 'admin';

			return;

		}

		elseif ($this->CI->uri->segment(1) == 'admin_install') {

			return;

		}

		else {

			if ($this->CI->uri->segment(1)) {

				$site = $this->CI->mdl_sites->get(array('url_title'=>$this->CI->uri->segment(1)));

			}

			if (!isset($site) OR !$site) {

				$site = $this->CI->mdl_sites->get(array('is_default'=>TRUE));

			}

			$this->site_id = $site->site_id;

			if (!$this->CI->session->userdata('is_admin')) {

				$this->CI->session->set_userdata('site_id', $site->site_id);

			}

			$this->site_name = $site->site_name;

			$this->site_description = $site->site_description;

			$this->site_url_title = $site->url_title;

			$this->site_template_folder = $site->template_folder;
			
			$this->site_template_default_view = $site->template_default_view;

		}

		if ($this->is_logged_in()) {
			
			$this->user_display_name = $this->CI->session->userdata('display_name');
			
		}

	}

	function view_file() {

		if ($this->CI->uri->segment(2)) {

			return $this->CI->uri->segment(2);

		}

		else {

			return $this->site_template_default_view;

		}

	}

	function category_links($post, $view) {

		$categories = array();

		foreach ($post->categories as $category) {

			$categories[] = $this->anchor($view . '/' . $category['url_title'], $category['category']);

		}

		return implode(', ', $categories);

	}

	function verify_delete() {

		return "javascript:if(!confirm('Are you sure you want to delete this item?')) return false";

	}

	function form_open_search($view) {

		$return = form_open($this->CI->uri->uri_string);

		$return .= form_hidden('result_view', $view);

		return $return;

	}

}

?>